package com.fisicaapp;


import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

@SuppressLint("WrongCall")
public class MecView extends SurfaceView {
	private Bitmap bmp;
	private SurfaceHolder holder ;
	animaMec mecHilo;
	static int masainicial = 10;
	static double altura = 30;
	static double alturaaux = altura;
	private float y =30;
	private float max = 30; 
	private float vel = 0;
	Paint pincelRojo = new Paint();
	Paint pincelAzul = new Paint();
	Paint pincelMagenta = new Paint();
	Energia energia;
	 
	
	
	public MecView(Context context) {
		super(context);
		mecHilo = new animaMec(this);
		energia = new Energia(altura,masainicial );
		   pincelRojo.setColor(Color.RED);
	        pincelRojo.setStrokeWidth(30);
	        pincelRojo.setStyle(Paint.Style.STROKE);
	        pincelAzul.setColor(Color.BLUE);
	        pincelAzul.setStrokeWidth(30);
	        pincelAzul.setStyle(Paint.Style.STROKE);
	        pincelMagenta.setColor(Color.MAGENTA);
	        pincelMagenta.setStrokeWidth(30);
	        pincelMagenta.setStyle(Paint.Style.STROKE);
		

		holder = getHolder();
		holder.addCallback(new SurfaceHolder.Callback() {
			
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				mecHilo.pintar();
			
				//mecHilo.start();
				//mecHilo.setRunning(true);
			}
			
			@Override 
			public void surfaceChanged(SurfaceHolder holder, int format, int width,
					int height) {
				
					
			
				
			}
		
		});
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
		// TODO Auto-generated constructor stub
	}
	public  void renuAnima(){
		mecHilo = new animaMec(this);
		mecHilo.start();
		mecHilo.setRunning(true); 
	}
	public  void pausaAnima(){
		mecHilo.setRunning(false); 
	}
	

	protected void onDraw(Canvas canvas) {
		if (alturaaux > 5){
		if (y > getHeight() - bmp.getHeight()) {
			max = (int) (max + (getHeight() - max) * (0.1));
			vel = (-1) * vel;
			alturaaux = alturaaux * 0.9;
			energia.setPerdidaEnergia(alturaaux);
		} 	 

		vel = (float) vel + (float) (0.981 /energia.getTiempo() );
		y = y + vel;

		if (y <= max) {
			y = max;
		}
		
		float EpAltura = (getHeight()* (float)(1-(energia.getEpotencial()/energia.getEtotal() )))+(max+bmp.getHeight())* (float)(energia.getEpotencial()/energia.getEtotal() );
		float EcAltura = (getHeight()* (float)(1-(energia.getEcinetica()/energia.getEtotal() )))+(max+bmp.getHeight())* (float)(energia.getEcinetica()/energia.getEtotal() ); 
		canvas.drawColor(Color.WHITE);	       
      	canvas.drawLine(60, getHeight() , 60,  EpAltura, pincelRojo);
      	canvas.drawLine(120, getHeight() , 120,EcAltura, pincelAzul);
      	
      	canvas.drawLine(180, getHeight() , 180, max+bmp.getHeight() , pincelMagenta);

    	energia.updateAltura(alturaaux*(((getHeight()- bmp.getHeight())-y) /((getHeight()- bmp.getHeight())-max) ) ); 	 
  
		canvas.drawBitmap(bmp, (getWidth()- bmp.getWidth()-30 ), y, null);}

		else{
			pausaAnima();
		}
		
	}
	public String getEnergiapotencialinicial(){		
		int ep = (int )energia.getEpotencial();
		return "Energia Pot :"+ep;
	}
	
	public String getEnergiapotencial(){	
		int ep = (int )energia.getEpotencial();
		return "Energia Pot :"+ep;
	}
	public CharSequence getEnergiaCinetica() {
		int ec = (int )energia.getEcinetica();
		return "Energia cin :"+ec;
		
	}
	public CharSequence getEnergiaMecanica() {
		int em = (int )energia.getEtotal();
		return "Energia Mec :"+em;
		 
	}
	public double[] getdatos() {
		double[] arraydatos = { alturaaux*(((getHeight()- bmp.getHeight())-y) /((getHeight()- bmp.getHeight())-max) ) , energia.getEpotencial() ,energia.getEcinetica(),masainicial};
		return arraydatos;
	
	}
}